<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="关键字:" prop="name">
        <el-input
          size="mini"
          v-model="queryParams.name"
          placeholder="请输入关键字"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属级别" prop="type">
        <el-select
          size="mini"
          v-model="queryParams.type"
          placeholder="操作状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.adduct_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini" v-hasPermi="['system:adduct:add']"
          @click="handleAdd"
        >添加</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="归属级别" :show-overflow-tooltip="true" prop="type" key="type" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.adduct_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="上级名称" :show-overflow-tooltip="true" prop="name" key="name" align="center"/>
      <el-table-column label="跳转地址" :show-overflow-tooltip="true" prop="url" key="url" align="center"/>
      <el-table-column label="排序号" align="center" prop="sort"/>
      <el-table-column label="创建时间" prop="createTime" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" class-name="small-padding fixed-width" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row)"
            v-hasPermi="['system:adduct:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:adduct:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="归属级别:" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="dict in dict.type.adduct_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上级名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入上级名称" />
        </el-form-item>
        <el-form-item label="跳转地址:" prop="url">
          <el-input v-model="form.url" placeholder="请输入跳转地址" />
        </el-form-item>
        <el-form-item label="显示排序:" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, edit, pageList, getInfo, remove } from '@/api/triones/adduct'
export default {
  name: "index",
  dicts: ['adduct_type'],
  data() {
    return {
      loading: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined,
        type: undefined
      },
      dataList: [],
      total: 0,
      title: null,
      open: false,
      form: {},
      rules: {
        type: [
          { required: true, message: "归属级别不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "上级名称不能为空", trigger: "blur" }
        ],
        url: [
          { required: true, message: "跳转地址不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 广告列表 */
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    /** 检索 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd() {
      this.reset();
      this.title = '添加上级政策汇集';
      this.open = true;
    },
    handleEdit(row) {
      this.reset();
      const id = row.id;
      getInfo(id).then(response => {
        this.form = response.data;
        this.title = '修改上级政策汇集';
        this.open = true;
      })
    },
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项？').then(() => {
        this.loading = true;
        return remove(id);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        url: undefined,
        sort: 0,
        type: '1'
      }
      this.resetForm("form");
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.open = true;
          if (this.form.id != null) {
            edit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
